const router = require('koa-router')();
const jwt = require('jsonwebtoken')
const {
    JWT_CONF
} = require('../utils/config')
const {
    md5
} = require('../utils/index')
const RespModel = require('../model/RespModel');
const {
    add,
    getUser,
    getList,
    updatePwd,
    delUser
} = require('../services/user');
router.prefix('/user');
router.post('/login', async (ctx, next) => {
    const {
        username,
        userpassword
    } = ctx.request.body;
    if (!username || !userpassword) {
        ctx.body = new RespModel('用户名或密码不能为空').error();
        return;

    }
    const user = await getUser(username);
    console.log('user', user)
    if (!user) {
        ctx.body = new RespModel('用户不存在').error();
        return;

    }
    //验证密码是否正确
    if (user.userpassword !== md5(userpassword)) {
        ctx.body = new RespModel('密码错误').error();
        return;
    }
    if (!user.status) {
        ctx.body = new RespModel('当前用户已被禁用,无法登陆!').error();
        return;
    }
    //设置jwttoken信息
    const token = 'Bearer ' + jwt.sign({
        username
    }, JWT_CONF.JWT_PRIVATE_KEY, {
        expiresIn: JWT_CONF.JWT_EXPIRED
    })
    ctx.body = new RespModel(null, '登录成功', {
        token: token,
        expiresIn: JWT_CONF.JWT_EXPIRED
    }).success();
})
//注册
router.post('/register', async (ctx, next) => {
    if (ctx.request.body) {
        const user = ctx.request.body;
        const result = await add(user);
        console.log(result)
        if (result.affectedRows > 0) {
            ctx.body = new RespModel('注册成功').success();
            return;
        } else {
            ctx.body = new RespModel('注册失败').error();
            return;
        }


    } else {
        ctx.body = new RespModel('参数不能为空').error();
    }
})
router.get('/info', async (ctx, next) => {
    const data = await getUser(ctx.state.user.username);
    let newModel = {};
    newModel.userName = data.userName;
    newModel.nickName = data.nickName;
    newModel.realName = data.realName;
    newModel.status = data.status;
    ctx.body = new RespModel(newModel, '查询成功').success();
    return;


})
router.get('/list', async (ctx, next) => {

    const data =await getList();
    ctx.body = new RespModel(data, '查询成功').success();
    return;

})

//修改密码
router.post('/updatePwd', async (ctx, next) => {

    const {
        userpassword
    } = ctx.request.body;
    if (!userpassword) {
        ctx.body = new RespModel('参数不能为空').error();
        return;
    }
    const result =await updatePwd(ctx.state.user.username, userpassword);
    if (result.affectedRows > 0) {
        ctx.body = new RespModel('密码修改成功').success();
        return;
    } else {
        ctx.body = new RespModel('密码修改失败').error();
    }


})
//删除用户
router.post('/delete',async (ctx, next) => {
    const {
        username
    } = ctx.request.body;
    if (!username) {
        ctx.body=new RespModel('参数不能为空').error();
        return;
    }
    //不能删当前用户
    if (username === ctx.state.user.username) {
    ctx.body=new RespModel('不能删除当前登录用户').error();
    }
   const result=await delUser(username);
        if (result.affectedRows > 0) {
        ctx.body=new RespModel('删除成功').success();
        } else {
            ctx.body=new RespModel('删除失败').error()
        }
    
})
module.exports = router;